/*     */ package com.java.diagnostics.visualizer.impl.properties.ids;
/*     */ 
/*     */ import com.java.diagnostics.visualizer.data.ids.ID;
/*     */ import com.java.diagnostics.visualizer.impl.GCAndMemoryVisualizerImplPreferenceInitalizer;
/*     */ import com.java.diagnostics.visualizer.impl.data.ids.IDImpl;
/*     */ import com.java.diagnostics.visualizer.impl.factory.LogFactory;
/*     */ import com.java.diagnostics.visualizer.impl.preferences.GCAndMemoryVisualizerPreferenceInitializer;
/*     */ import com.java.diagnostics.visualizer.impl.preferences.PreferencesImportListener;
/*     */ import com.java.diagnostics.visualizer.impl.preferences.SmartPreferences;
/*     */ import com.java.diagnostics.visualizer.impl.properties.OutputPropertiesImpl;
/*     */ import com.java.diagnostics.visualizer.properties.IDsToDisplayProperties;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.Set;
/*     */ import java.util.logging.Level;
/*     */ import java.util.logging.Logger;
/*     */ import org.eclipse.core.runtime.Platform;
/*     */ import org.eclipse.core.runtime.preferences.IScopeContext;
/*     */ 
/*     */ public abstract class IDsToDisplayPropertiesImpl
/*     */   implements IDsToDisplayProperties, PreferencesImportListener
/*     */ {
/*     */   private static final String SPLITTING_REGEX = "[\\[,\\]]";
/*     */   private static final String CLOSE_BRACKET = "]";
/*     */   private static final String COMMA = ", ";
/*     */   private static final String OPEN_BRACKET = "[";
/*  59 */   private static final Logger TRACE = LogFactory.getTrace(IDsToDisplayPropertiesImpl.class);
/*     */   private Set ids;
/*     */   private SmartPreferences preferences;
/*     */   private OutputPropertiesImpl props;
/*     */   private boolean notificationsEnabled;
/*     */   private boolean preferencesEnabled;
/*     */   protected String key;
/*     */ 
/*     */   protected IDsToDisplayPropertiesImpl(String key, IScopeContext scope, OutputPropertiesImpl parent)
/*     */   {
/*  80 */     this.key = key;
/*  81 */     this.props = parent;
/*  82 */     this.notificationsEnabled = true;
/*  83 */     this.preferencesEnabled = true;
/*     */ 
/*  89 */     if (Platform.isRunning()) {
/*  90 */       this.preferences = 
/*  91 */         GCAndMemoryVisualizerImplPreferenceInitalizer.getInstance().getPreferences(scope, scope);
/*  92 */       refreshFromPreferences();
/*  93 */       this.preferences.addPreferenceListener(this);
/*     */     } else {
/*  95 */       this.ids = new HashSet();
/*     */     }
/*     */ 
/*  99 */     updatePreferences();
/*     */   }
/*     */ 
/*     */   private void refreshFromPreferences() {
/* 103 */     String values = this.preferences.getString(this.key);
/* 104 */     this.ids = destringify(values);
/* 105 */     if (TRACE.isLoggable(Level.FINE))
/* 106 */       TRACE.fine("Read preferences and got ids " + values);
/*     */   }
/*     */ 
/*     */   public boolean isEnabled(ID id)
/*     */   {
/* 117 */     return this.ids.contains(id);
/*     */   }
/*     */ 
/*     */   public void enablePreferences()
/*     */   {
/* 122 */     this.preferencesEnabled = true;
/*     */   }
/*     */ 
/*     */   public void disablePreferences()
/*     */   {
/* 127 */     this.preferencesEnabled = false;
/*     */   }
/*     */ 
/*     */   public Iterator getAllIDsToDisplay()
/*     */   {
/* 136 */     return this.ids.iterator();
/*     */   }
/*     */ 
/*     */   public void addID(ID id)
/*     */   {
/* 145 */     this.ids.add(id);
/* 146 */     updatePreferences();
/*     */   }
/*     */ 
/*     */   public void removeID(ID id)
/*     */   {
/* 155 */     this.ids.remove(id);
/* 156 */     updatePreferences();
/*     */   }
/*     */ 
/*     */   private void updatePreferences() {
/* 160 */     if (this.preferencesEnabled) {
/* 161 */       if (this.preferences != null) {
/* 162 */         String string = stringify(this.ids);
/* 163 */         this.preferences.setValue(this.key, string);
/*     */       }
/*     */ 
/* 166 */       if (this.notificationsEnabled)
/* 167 */         this.props.notifyListeners();
/*     */     }
/*     */   }
/*     */ 
/*     */   public void removeAllIDs()
/*     */   {
/* 183 */     this.ids = new HashSet();
/* 184 */     updatePreferences();
/*     */   }
/*     */ 
/*     */   protected static String stringify(Set set)
/*     */   {
/* 189 */     StringBuffer buffer = new StringBuffer();
/* 190 */     buffer.append("[");
/* 191 */     Iterator it = set.iterator();
/* 192 */     while (it.hasNext()) {
/* 193 */       buffer.append(it.next());
/* 194 */       if (it.hasNext()) {
/* 195 */         buffer.append(", ");
/*     */       }
/*     */     }
/* 198 */     buffer.append("]");
/*     */ 
/* 200 */     return buffer.toString();
/*     */   }
/*     */ 
/*     */   protected Set destringify(String string)
/*     */   {
/* 205 */     Set set = new HashSet();
/* 206 */     if (string.length() > 0) {
/* 207 */       String[] split = string.split("[\\[,\\]]");
/* 208 */       for (int i = 0; i < split.length; i++)
/*     */       {
/* 210 */         if (split[i].length() > 0) {
/* 211 */           Object thing = constructIDFromString(split[i]);
/* 212 */           set.add(thing);
/*     */         }
/*     */       }
/*     */     }
/*     */ 
/* 217 */     return set;
/*     */   }
/*     */ 
/*     */   private final IDImpl constructIDFromString(String string) {
/* 221 */     return IDImpl.fromString(string);
/*     */   }
/*     */ 
/*     */   public void preferenceChange() {
/* 225 */     refreshFromPreferences();
/*     */   }
/*     */ 
/*     */   public void disableNotifications()
/*     */   {
/* 230 */     this.notificationsEnabled = false;
/* 231 */     this.preferencesEnabled = false;
/*     */   }
/*     */ 
/*     */   public void enableNotifications() {
/* 235 */     this.notificationsEnabled = true;
/* 236 */     this.preferencesEnabled = true;
/*     */   }
/*     */ }

